<template>
    <div class="goods-container">
        <transition
                @before-enter='beforeEnter'
                @enter="enter"
                @after-enter='afterEnter'
                @after-leave='afterLeave'>
            <div class="ball" v-show="flag" ref="ball"></div>
        </transition>

        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <swiper :lunBoList="lunBoList"></swiper>
                </div>
            </div>
        </div>
        <div class="mui-card">
            <div class="mui-card-header">小米 红米Redmi Note7</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        销售价：<span class="now">￥{{price}}</span>
                        市场价：
                        <del>￥2399</del>

                    </p>
                    <p>购买数量：
                        <numbox id="kuang" @getNum='getSelectedNum' :max="number"></numbox>
                    </p>
                    <p class="btn">
                        <mt-button type="primary" size="small">立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="clickFlag&&showBall()">加入购物车</mt-button>
                    </p>
                </div>
            </div>
        </div>
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号：XM-190402-01</p>
                    <p>库存情况：{{ number }}件</p>
                    <p>上架时间：2019年4月2日</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain>查看商品详情</mt-button>
                <mt-button type="danger" size="large" plain>查看商品评论</mt-button>
            </div>
        </div>
    </div>
</template>

<script>
    import swiper from '../subComponents/swiper.vue';
    import numbox from '../subComponents/numbox.vue';

    export default {
        data() {
            return {
                lunBoList: [],
                flag: false,
                clickFlag: true,
                selectedNum: 1,
                number: 33,
                id:1,
                price:999,
            }
        },
        created() {
            this.getLunBo();
        },
        methods: {
            getLunBo() {
                this.$http.get('slides').then(result => {
                    if (result.status === 200) {
                        this.lunBoList = result.body;
                    } else {
                        Toast('网络较差，请稍候再试')
                    }
                })
            },
            showBall() {
                this.flag = !this.flag;
                //保存到共享区域的数据格式
                var  goodsinfo = {id:this.id,count:this.selectedNum,price:this.price,selected:true};
                this.$store.commit('addToCar',goodsinfo);

            },
            //小球的动画
            beforeEnter(el) {
                //解决起点变化的问题：根据加减框的位置，动态的设定小球的绝对定位
                const kuangPosition = document.getElementById('kuang').getBoundingClientRect();
                const xDist1 = kuangPosition.left + 43;
                const yDist1 = kuangPosition.top + 10;
                this.$refs.ball.style.top = `${yDist1}px`;
                this.$refs.ball.style.left = `${xDist1}px`;


                el.style.transform = 'translate(0,0)';
            },
            enter(el, done) {
                this.clickFlag = false;
                el.offsetWidth;

                //解决终点变化的问题
                const ballPosition = this.$refs.ball.getBoundingClientRect();
                const badgePosition = document.getElementById('badge').getBoundingClientRect();
                const xDist = badgePosition.left - ballPosition.left;
                const yDist = badgePosition.top - ballPosition.top ;


                el.style.transform = `translate(${xDist}px,${yDist}px)`;
                el.style.transition = "all 0.3s cubic-bezier(.4,-0.3,1,.68)";
                done();
            },
            afterEnter(el) {
                this.flag = !this.flag;
            },
            afterLeave(el) {          //解决小球出去会回来的现象；只有clickFlag为true的时候，才会再次显示小球
                this.clickFlag = true;
            },

            //设置购买数量的最大值
            getSelectedNum(num) {
                this.selectedNum = num;
            }

        },
        components: {
            swiper,
            numbox,
        }
    }

</script>

<style lang="less" scoped>
    .goods-container {
        background-color: #ccc;
        overflow: hidden;

        .price {
            .now {
                color: red;
                font-size: 16px;
                margin-right: 15px;
            }
        }

        .btn {
            button {
                margin-left: 15px;
            }

        }

        .mui-numbox {
            margin-left: 10px;
            height: 30px;
        }

        .mui-card-footer {
            display: block;

            button {
                margin-bottom: 10px;
            }
        }

        .ball {
            width: 15px;
            height: 15px;
            border-radius: 50%;
            background-color: red;
            position: absolute;
            z-index: 99;
            /*top: 390px;*/
            /*left: 160px;*/
        }
    }
</style>